﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YArchitech.HVAC
{
	public partial class FormMarkSetting : System.Windows.Forms.Form
	{
		public FormMarkSetting(ShowMarkType showType)
		{
			this.InitializeComponent();
			YJKProcessTools hyprocessTools = new YJKProcessTools();
			hyprocessTools.ShowInTaskbarChange(this);
			//base.Icon = Resource.YArchitechIcon;
			this.userSetting = new UserSetting();
			this.markType = showType;
		}

		private void FormMarkSetting_Load(object sender, EventArgs e)
		{
			this.InitialInterfaceDatas();
			this.SetGroupBoxEnable();
			if (this.markType == ShowMarkType.Diameter)
			{
				if (this.ckbPipeOffset.Checked)
				{
					this.grpPipeOffsetMark.Enabled = true;
				}
				else
				{
					this.grpPipeOffsetMark.Enabled = false;
				}
			}
			this.isLoad = false;
		}

		public string GetMarkAccuracy()
		{
			return this.accuracy;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnSure_Click(object sender, EventArgs e)
		{
			this.SaveInterfaceDatas();
			base.DialogResult = DialogResult.OK;
		}

		private void btnSettingPrefix_Click(object sender, EventArgs e)
		{
			FormPrefixSetting formPrefixSetting = new FormPrefixSetting();
			formPrefixSetting.ShowDialog();
		}

		private void rdoRelative_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoRelative.Checked)
			{
				this.cmbOffsetPrefix.Enabled = true;
			}
			if (this.rdoAbsolute.Checked)
			{
				this.cmbOffsetPrefix.Enabled = false;
			}
		}

		private void rdoObselutePipe_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoObselutePipe.Checked)
			{
				this.cmbPipeOffsetPrefix.Enabled = false;
			}
			else if (this.rdoPipeRelative.Checked)
			{
				this.cmbPipeOffsetPrefix.Enabled = true;
			}
			if (this.markType != ShowMarkType.Diameter)
			{
				return;
			}
			this.cmbPipeSpliteChar.Enabled = true;
		}

		private void InitialInterfaceDatas()
		{
			this.InitialFontSetting();
			this.InitialDuctMarkSetting();
			this.InitialTeminalMarkSetting();
			this.InitialDiameterMarkSetting();
			this.InitialOffsetMarkSetting();
			this.InitialSlopeMarkSetting();
		}

		private void SaveInterfaceDatas()
		{
			this.userSetting = new UserSetting();
			this.SaveFontSetting();
			this.SaveDuctMarkSetting();
			this.SaveTeminalMarkSetting();
			this.SaveDiameterMarkSetting();
			this.SaveOffsetMarkSetting();
			this.SaveSlopeMarkSetting();
		}

		private void SetGroupBoxEnable()
		{
			List<GroupBox> setGroupBoxs = this.GetSetGroupBoxs();
			switch (this.markType)
			{
			case ShowMarkType.Font:
				this.SetSpectialGroupEnable(setGroupBoxs, this.hyGrpMarkSetting);
				this.tabControl1.SelectedIndex = 0;
				return;
			case ShowMarkType.Duct:
				this.SetSpectialGroupEnable(setGroupBoxs, this.hyGrpDuctMark);
				this.tabControl1.SelectedIndex = 0;
				return;
			case ShowMarkType.Terminal:
				this.SetSpectialGroupEnable(setGroupBoxs, this.hyGrpTerminalMark);
				this.tabControl1.SelectedIndex = 0;
				return;
			case ShowMarkType.Diameter:
				this.SetSpectialGroupEnable(setGroupBoxs, this.hyGrpDiameterMark);
				this.grpPipeOffsetMark.Enabled = true;
				this.SetDiameterEnabled();
				this.tabControl1.SelectedIndex = 1;
				return;
			case ShowMarkType.Offset:
				this.SetSpectialGroupEnable(setGroupBoxs, this.grpPipeOffsetMark);
				this.tabControl1.SelectedIndex = 1;
				return;
			case ShowMarkType.Slope:
				this.SetSpectialGroupEnable(setGroupBoxs, this.grpSlopeMark);
				this.tabControl1.SelectedIndex = 1;
				this.SetPipeSlopeEnabled(this.beEnableSlope);
				return;
			default:
				this.SetSpectialGroupEnable(setGroupBoxs, null);
				this.tabControl1.SelectedIndex = 0;
				return;
			}
		}

		public void SetDrawSlidIsEnabled(bool isEnabled)
		{
			this.isDrawSlidEnabled = isEnabled;
		}

		public void SetDiameterOtherEnabled(bool isEnabled)
		{
			this.isDiameterOtherEnabled = isEnabled;
		}

		public void SetDiameterEnabled()
		{
			this.ckbDrawSlash.Enabled = this.isDrawSlidEnabled;
			this.ckbMarkTowSides.Enabled = this.isDiameterOtherEnabled;
			this.ckbMarkReduce.Enabled = this.isDiameterOtherEnabled;
			this.label17.Enabled = this.isDiameterOtherEnabled;
			this.hyTxtMiniLength.Enabled = this.isDiameterOtherEnabled;
			this.label18.Enabled = this.isDiameterOtherEnabled;
			this.hyTxtMiniDiameter.Enabled = this.isDiameterOtherEnabled;
			if (this.rdoPipeRelative.Checked)
			{
				this.cmbPipeSpliteChar.Enabled = true;
			}
			this.cmbPipeConnect.Enabled = this.ckbThickness.Checked;
		}

		public void SetPipeSlopeEnabled(bool beEnableed)
		{
			this.cmbSlopeStyle.Enabled = !beEnableed;
		}

		public void SetDrawSlashEnable(bool beEnable)
		{
			this.ckbDrawSlash.Enabled = beEnable;
		}

		private void SetSpectialGroupEnable(List<GroupBox> listGrp, GroupBox grpBox)
		{
			if (grpBox == null)
			{
				using (List<GroupBox>.Enumerator enumerator = listGrp.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						GroupBox groupBox = enumerator.Current;
						groupBox.Enabled = true;
					}
					return;
				}
			}
			foreach (GroupBox groupBox2 in listGrp)
			{
				if (groupBox2.Name.Equals(grpBox.Name))
				{
					groupBox2.Enabled = true;
				}
				else
				{
					groupBox2.Enabled = false;
				}
			}
		}

		private List<GroupBox> GetSetGroupBoxs()
		{
			return new List<GroupBox>
			{
				this.hyGrpMarkSetting,
				this.hyGrpDuctMark,
				this.hyGrpTerminalMark,
				this.hyGrpDiameterMark,
				this.grpPipeOffsetMark,
				this.grpSlopeMark
			};
		}

		private void InitialFontSetting()
		{
			CommonFont commonFont = new CommonFont();
			commonFont = this.userSetting.GetFontSetting();
			this.BindingFontStyleCombox(commonFont.Font);
			this.hyTxtHeight.Text = commonFont.FontHeight.ToString();
			this.hyTxtWHRotia.Text = commonFont.FontGWRotia.ToString();
			this.cmbAlign.SelectedItem = commonFont.Align;
			this.hyTxtLeadRotia.Text = commonFont.LeadRotia.ToString();
			this.cmbPresice.SelectedItem = commonFont.Presice;
			this.hyTxtDistanceLead.Text = commonFont.LeadDistance.ToString();
			this.hyTxtDistancPipe.Text = commonFont.TubeDistance.ToString();
			this.oldFontHeight = commonFont.FontHeight.ToString();
			this.oldFontWHRotia = commonFont.FontGWRotia.ToString();
			this.oldLeadRotia = commonFont.LeadRotia.ToString();
			this.oldDistanceLead = commonFont.LeadDistance.ToString();
			this.oldDistancPipe = commonFont.TubeDistance.ToString();
		}

		private void InitialDuctMarkSetting()
		{
			DuctMarkSet ductMarkSetting = this.userSetting.GetDuctMarkSetting();
			if (ductMarkSetting.MarkContent.Equals(this.rdoAbsolute.Text))
			{
				this.rdoAbsolute.Checked = true;
				this.rdoRelative.Checked = false;
				this.cmbOffsetPrefix.Enabled = false;
			}
			else if (ductMarkSetting.MarkContent.Equals(this.rdoRelative.Text))
			{
				this.rdoAbsolute.Checked = false;
				this.rdoRelative.Checked = true;
				this.cmbOffsetPrefix.Enabled = true;
			}
			this.cmbRoundDuctPrefix.Text = ductMarkSetting.DuctPrefix;
			this.cmbRoundDuctSuffix.Text = ductMarkSetting.DuctSuffix;
			this.cmbOtherSpilte.Text = (string.IsNullOrEmpty(ductMarkSetting.Splite) ? "，" : ductMarkSetting.Splite);
			this.cmbOffsetPrefix.Text = ductMarkSetting.OffsetPrefix;
			this.cmbOffsetSuffix.Text = ductMarkSetting.OffsetSuffix;
			this.hyTxtOtherMiniLength.Text = ductMarkSetting.TubeLength.ToString();
			this.oldOtherMiniLength = ductMarkSetting.TubeLength.ToString();
		}

		private void InitialTeminalMarkSetting()
		{
			TerminalMarkSet terminalMarkSetting = this.userSetting.GetTerminalMarkSetting();
			if (this.CheckStringBeTrue(terminalMarkSetting.DrawLineBox))
			{
				this.ckbDrawLineBox.Checked = true;
			}
			else
			{
				this.ckbDrawLineBox.Checked = false;
			}
			this.cmbTerminalSplite.Text = terminalMarkSetting.Splite;
		}

		private void InitialDiameterMarkSetting()
		{
			PipeDiameterMarkSet diameterMarkSetting = this.userSetting.GetDiameterMarkSetting();
			this.ckbPipeSystemType.Checked = this.CheckStringBeTrue(diameterMarkSetting.SystemType);
			this.ckbThickness.Checked = this.CheckStringBeTrue(diameterMarkSetting.Thickness);
			this.cmbPipeConnect.Text = diameterMarkSetting.Splite;
			this.ckbPipeOffset.Checked = this.CheckStringBeTrue(diameterMarkSetting.PipeOffset);
			this.ckbDrawSlash.Checked = this.CheckStringBeTrue(diameterMarkSetting.DrawSlash);
			this.ckbMarkTowSides.Checked = this.CheckStringBeTrue(diameterMarkSetting.TwoSides);
			this.ckbMarkReduce.Checked = this.CheckStringBeTrue(diameterMarkSetting.Reduce);
			this.hyTxtMiniLength.Text = diameterMarkSetting.TubeLength.ToString();
			this.hyTxtMiniDiameter.Text = diameterMarkSetting.TubeDiameter.ToString();
			this.cmbPipeDiameter.Text = diameterMarkSetting.Diameter;
			this.oldTxtMiniLength = diameterMarkSetting.TubeLength.ToString();
			this.oldTxtMiniDiameter = diameterMarkSetting.TubeDiameter.ToString();
		}

		private void InitialOffsetMarkSetting()
		{
			PipeOffsetMarkSet offsetMarkSetting = this.userSetting.GetOffsetMarkSetting();
			if (offsetMarkSetting.Content.Equals(this.rdoObselutePipe.Text))
			{
				this.rdoObselutePipe.Checked = true;
				this.rdoPipeRelative.Checked = false;
				this.cmbPipeOffsetPrefix.Enabled = false;
			}
			else if (offsetMarkSetting.Content.Equals(this.rdoPipeRelative.Text))
			{
				this.rdoObselutePipe.Checked = false;
				this.rdoPipeRelative.Checked = true;
				this.cmbPipeOffsetPrefix.Enabled = true;
			}
			this.cmbPipeOffsetPrefix.Text = offsetMarkSetting.Prefix;
			this.cmbPipeSpliteChar.Text = offsetMarkSetting.Splite;
			this.cmbPipeOffsetSuffix.Text = offsetMarkSetting.Suffix;
		}

		private void InitialSlopeMarkSetting()
		{
			SlopeMarkSet slopeMarkSetting = this.userSetting.GetSlopeMarkSetting();
			this.cmbSlopeStyle.Text = slopeMarkSetting.SlopeStyle;
			this.cmbPipeStyle.Text = slopeMarkSetting.Style;
		}

		private bool CheckStringBeTrue(string strValue)
		{
			return strValue.Equals("true");
		}

		private void BindingFontStyleCombox(string selectedFontName)
		{
			List<string> windowsFontNames = this.GetWindowsFontNames();
			this.cmbTextFont.DataSource = windowsFontNames;
			this.cmbTextFont.Text = selectedFontName;
		}

		private List<string> GetWindowsFontNames()
		{
			InstalledFontCollection installedFontCollection = new InstalledFontCollection();
			FontFamily[] families = installedFontCollection.Families;
			List<string> list = new List<string>();
			int num = families.Length;
			for (int i = 0; i < num; i++)
			{
				string name = families[i].Name;
				list.Add(name);
			}
			list.Reverse();
			return list;
		}

		private void SaveFontSetting()
		{
			CommonFont commonFont = new CommonFont();
			commonFont.Font = this.cmbTextFont.Text;
			commonFont.FontHeight = this.GetDoubleValue(this.hyTxtHeight.Text);
			commonFont.FontGWRotia = this.GetDoubleValue(this.hyTxtWHRotia.Text);
			commonFont.Align = this.cmbAlign.Text;
			commonFont.LeadRotia = this.GetDoubleValue(this.hyTxtLeadRotia.Text);
			commonFont.Presice = this.cmbPresice.Text;
			commonFont.LeadDistance = this.GetDoubleValue(this.hyTxtDistanceLead.Text);
			commonFont.TubeDistance = this.GetDoubleValue(this.hyTxtDistancPipe.Text);
			this.userSetting.WriteFontSetting(commonFont);
		}

		private void SaveDuctMarkSetting()
		{
			DuctMarkSet ductMarkSet = new DuctMarkSet();
			if (this.rdoAbsolute.Checked)
			{
				ductMarkSet.MarkContent = this.rdoAbsolute.Text;
			}
			else
			{
				ductMarkSet.MarkContent = this.rdoRelative.Text;
			}
			ductMarkSet.DuctPrefix = this.cmbRoundDuctPrefix.Text;
			ductMarkSet.DuctSuffix = this.cmbRoundDuctSuffix.Text;
			ductMarkSet.Splite = this.cmbOtherSpilte.Text;
			ductMarkSet.OffsetPrefix = this.cmbOffsetPrefix.Text;
			ductMarkSet.OffsetSuffix = this.cmbOffsetSuffix.Text;
			ductMarkSet.TubeLength = this.GetDoubleValue(this.hyTxtOtherMiniLength.Text);
			this.userSetting.WriteDuctMarkSetting(ductMarkSet);
		}

		private void SaveTeminalMarkSetting()
		{
			TerminalMarkSet terminalMarkSet = new TerminalMarkSet();
			terminalMarkSet.DrawLineBox = this.GetStrValueByBool(this.ckbDrawLineBox.Checked);
			terminalMarkSet.Splite = this.cmbTerminalSplite.Text;
			this.userSetting.WriteTerminalMarkSetting(terminalMarkSet);
		}

		private void SaveDiameterMarkSetting()
		{
			PipeDiameterMarkSet pipeDiameterMarkSet = new PipeDiameterMarkSet();
			pipeDiameterMarkSet.SystemType = this.GetStrValueByBool(this.ckbPipeSystemType.Checked);
			pipeDiameterMarkSet.Thickness = this.GetStrValueByBool(this.ckbThickness.Checked);
			pipeDiameterMarkSet.Splite = this.cmbPipeConnect.Text;
			pipeDiameterMarkSet.PipeOffset = this.GetStrValueByBool(this.ckbPipeOffset.Checked);
			pipeDiameterMarkSet.DrawSlash = this.GetStrValueByBool(this.ckbDrawSlash.Checked);
			pipeDiameterMarkSet.TwoSides = this.GetStrValueByBool(this.ckbMarkTowSides.Checked);
			pipeDiameterMarkSet.Reduce = this.GetStrValueByBool(this.ckbMarkReduce.Checked);
			pipeDiameterMarkSet.TubeLength = this.GetDoubleValue(this.hyTxtMiniLength.Text);
			pipeDiameterMarkSet.TubeDiameter = this.GetDoubleValue(this.hyTxtMiniDiameter.Text);
			pipeDiameterMarkSet.Diameter = this.cmbPipeDiameter.Text;
			this.userSetting.WriteDiameterMarkSetting(pipeDiameterMarkSet);
		}

		private void SaveOffsetMarkSetting()
		{
			PipeOffsetMarkSet pipeOffsetMarkSet = new PipeOffsetMarkSet();
			if (this.rdoObselutePipe.Checked)
			{
				pipeOffsetMarkSet.Content = this.rdoObselutePipe.Text;
			}
			else
			{
				pipeOffsetMarkSet.Content = this.rdoPipeRelative.Text;
			}
			pipeOffsetMarkSet.Prefix = this.cmbPipeOffsetPrefix.Text;
			pipeOffsetMarkSet.Splite = this.cmbPipeSpliteChar.Text;
			pipeOffsetMarkSet.Suffix = this.cmbPipeOffsetSuffix.Text;
			this.userSetting.WriteOffsetMarkSetting(pipeOffsetMarkSet);
		}

		private void SaveSlopeMarkSetting()
		{
			SlopeMarkSet slopeMarkSet = new SlopeMarkSet();
			slopeMarkSet.SlopeStyle = this.cmbSlopeStyle.Text;
			slopeMarkSet.Style = this.cmbPipeStyle.Text;
			this.userSetting.WriteSlopeMarkSetting(slopeMarkSet);
		}

		private string GetStrValueByBool(bool beTrue)
		{
			if (!beTrue)
			{
				return "false";
			}
			return "true";
		}

		private double GetDoubleValue(string strValue)
		{
			double result;
			try
			{
				if (string.IsNullOrEmpty(strValue))
				{
					result = 0.0;
				}
				else
				{
					result = Convert.ToDouble(strValue);
				}
			}
			catch (Exception)
			{
				result = 0.0;
			}
			return result;
		}

		private void ckbPipeSystemType_CheckedChanged(object sender, EventArgs e)
		{
			if (this.ckbPipeSystemType.Checked || this.ckbPipeOffset.Checked)
			{
				this.ckbThickness.Checked = false;
				this.ckbThickness.Enabled = false;
				this.cmbPipeConnect.Enabled = this.ckbThickness.Enabled;
				if (this.markType != ShowMarkType.Diameter)
				{
					this.cmbPipeSpliteChar.Enabled = false;
				}
				else
				{
					this.cmbPipeSpliteChar.Enabled = true;
				}
			}
			else
			{
				this.ckbThickness.Enabled = true;
				this.cmbPipeConnect.Enabled = this.ckbThickness.Enabled;
				this.cmbPipeSpliteChar.Enabled = true;
			}
			if (this.ckbThickness.Checked)
			{
				this.ckbPipeSystemType.Checked = false;
				this.ckbPipeOffset.Checked = false;
				this.ckbPipeSystemType.Enabled = false;
				this.ckbPipeOffset.Enabled = false;
				this.cmbPipeSpliteChar.Enabled = true;
			}
			else
			{
				this.ckbPipeSystemType.Enabled = true;
				this.ckbPipeOffset.Enabled = true;
				this.cmbPipeSpliteChar.Enabled = false;
				if (this.markType == ShowMarkType.Diameter)
				{
					this.cmbPipeSpliteChar.Enabled = true;
				}
			}
			this.cmbPipeConnect.Enabled = this.ckbThickness.Checked;
			if (this.markType == ShowMarkType.Diameter)
			{
				if (this.ckbPipeOffset.Checked)
				{
					this.grpPipeOffsetMark.Enabled = true;
					return;
				}
				this.grpPipeOffsetMark.Enabled = false;
			}
		}

		private void ckbDrawLineBox_CheckedChanged(object sender, EventArgs e)
		{
			if (this.ckbDrawLineBox.Checked)
			{
				this.cmbTerminalSplite.Enabled = false;
				return;
			}
			this.cmbTerminalSplite.Enabled = true;
		}

		private void hyTxt_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == '.' || e.KeyChar == '\b')
			{
				e.Handled = false;
				return;
			}
			e.Handled = true;
		}

		private void hyTxt_Leave(object sender, EventArgs e)
		{
			Regex regex = new Regex("^[.]+[\\d]*$");
			TextBox textBox = (TextBox)sender;
			string text = ((TextBox)sender).Text;
			this.TrimInvalidPreffix(ref text, '0');
			this.TrimInvalidSuffix(ref text, '.');
			double num;
			bool flag = double.TryParse(text, out num);
			if (string.IsNullOrEmpty(text) || regex.IsMatch(text) || !flag)
			{
				textBox.Text = this.GetDefaultValue(textBox);
				return;
			}
			textBox.Text = text;
		}

		private void TrimInvalidPreffix(ref string input, char preffix)
		{
			if (string.IsNullOrEmpty(input))
			{
				return;
			}
			if (input.IndexOf(preffix) == 0 && (input.IndexOf('.') == -1 || input.IndexOf('.') > 1))
			{
				input = input.Substring(1, input.Length - 1);
				if (string.IsNullOrEmpty(input))
				{
					return;
				}
				this.TrimInvalidPreffix(ref input, preffix);
			}
		}

		private void TrimInvalidSuffix(ref string input, char suffix)
		{
			if (string.IsNullOrEmpty(input))
			{
				return;
			}
			if (input.LastIndexOf(suffix) == input.Length - 1)
			{
				input = input.TrimEnd(new char[]
				{
					suffix
				});
			}
		}

		private string GetDefaultValue(TextBox tb)
		{
			string empty = string.Empty;
			if (tb == null)
			{
				return empty;
			}
			string name;
			switch (name = tb.Name)
			{
			case "hyTxtHeight":
				empty = this.oldFontHeight;
				break;
			case "hyTxtWHRotia":
				empty = this.oldFontWHRotia;
				break;
			case "hyTxtLeadRotia":
				empty = this.oldLeadRotia;
				break;
			case "hyTxtDistanceLead":
				empty = this.oldDistanceLead;
				break;
			case "hyTxtDistancPipe":
				empty = this.oldDistancPipe;
				break;
			case "hyTxtOtherMiniLength":
				empty = this.oldOtherMiniLength;
				break;
			case "hyTxtMiniLength":
				empty = this.oldTxtMiniLength;
				break;
			case "hyTxtMiniDiameter":
				empty = this.oldTxtMiniDiameter;
				break;
			}
			return empty;
		}

		private void cmbRoundDuctPrefix_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (!this.isLoad)
			{
				YJKMessageBox.Information("“圆风管前缀”和“后缀”的设置，在Reivt功能区“管理-MEP设置-机械设置”里面的“圆形风管尺寸前缀”和“圆形风管尺寸后缀”中完成！");
			}
		}

		private void cmbRoundDuctSuffix_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (!this.isLoad)
			{
				YJKMessageBox.Information("“圆风管前缀”和“后缀”的设置，在Reivt功能区“管理-MEP设置-机械设置”里面的“圆形风管尺寸前缀”和“圆形风管尺寸后缀”中完成！");
			}
		}

		private void cmbPresice_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.accuracy = this.cmbPresice.Text;
		}

		private bool isLoad = true;

		private UserSetting userSetting;

		private ShowMarkType markType = ShowMarkType.InValid;

		private bool isDrawSlidEnabled;

		private bool isDiameterOtherEnabled;

		public bool beEnableSlope = true;

		private string accuracy = "0.001";

		private string oldFontHeight = string.Empty;

		private string oldFontWHRotia = string.Empty;

		private string oldLeadRotia = string.Empty;

		private string oldDistanceLead = string.Empty;

		private string oldDistancPipe = string.Empty;

		private string oldOtherMiniLength = string.Empty;

		private string oldTxtMiniLength = string.Empty;

		private string oldTxtMiniDiameter = string.Empty;
	}
}
